home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / WLIB.ZIP / WSTR.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-30  |  12.2 KB  |  303 lines

  1. #ifndef WStrIncluded
  2. #define WStrIncluded
  3.  
  4. // copyright (c) 1992, 1993 by Paul Wheaton
  5. // 1916 Brooks #205, Missoula, MT  59801
  6. //
  7. //       phone:  (406)543-1928
  8. //  CompuServe:  72707,207
  9. //    Internet:  72707.207@CompuServe.com
  10.  
  11. #include <string.h>
  12. #include <WMisc.h>
  13. #include <stdlib.h>
  14.  
  15. #define DefaultStringExtra    15
  16.   /*  used internally by "String". Since most strings are very small yet
  17.   will have a little bit more added to them, this extra allocation should
  18.   save quite a bit of time.  The only time it would be a hendrance is when
  19.   an array of strings is created */
  20.  
  21. class String;
  22. class StackString;
  23. class String40;
  24. class String120;
  25.  
  26. class BaseString
  27.   {
  28.     protected:
  29.       char* P;   // character string
  30.       int Len;   // Length of string, excluding null character
  31.       int Alloc; // amount of actual storage allocated (including null char)
  32.       friend String;
  33.       friend StackString;
  34.       friend String40;
  35.       friend String120;
  36.     public:
  37.       operator const char*() const {return P;}
  38.       char& operator[](int Index);
  39.       Bool operator<(const BaseString& S) const { return strcmp(P, S.P) < 0; }
  40.       Bool operator>(const BaseString& S) const { return strcmp(P, S.P) > 0; }
  41.       Bool operator<=(const BaseString& S) const { return strcmp(P, S.P) <= 0; }
  42.       Bool operator>=(const BaseString& S) const { return strcmp(P, S.P) >= 0; }
  43.       Bool operator==(const BaseString& S) const;
  44.       Bool operator!=(const BaseString& S) const { return !(*this==S); }
  45.  
  46.       Bool operator<(const char* CS)  const { return strcmp(P,CS) < 0; }
  47.       Bool operator>(const char* CS)  const { return strcmp(P,CS) > 0; }
  48.       Bool operator<=(const char* CS) const { return strcmp(P,CS) <= 0; }
  49.       Bool operator>=(const char* CS) const { return strcmp(P,CS) >= 0; }
  50.       Bool operator==(const char* CS) const { return strcmp(P,CS) == 0; }
  51.       Bool operator!=(const char* CS) const { return strcmp(P,CS) != 0; }
  52.  
  53.       friend Bool operator<(const char* cs, const BaseString& S)
  54.         { return strcmp(cs, (S.P)) < 0; }
  55.       friend Bool operator>(const char* cs, const BaseString& S)
  56.         { return strcmp(cs, (S.P)) > 0; }
  57.       friend Bool operator<=(const char* cs, const BaseString& S)
  58.         { return strcmp(cs, (S.P)) <= 0; }
  59.       friend Bool operator>=(const char* cs, const BaseString& S)
  60.         { return strcmp(cs, (S.P)) >= 0; }
  61.       friend Bool operator==(const char* cs, const BaseString& S)
  62.         { return strcmp(cs, (S.P)) == 0; }
  63.       friend Bool operator!=(const char* cs, const BaseString& S)
  64.         { return strcmp(cs, (S.P)) != 0; }
  65.  
  66.       int Length() const { return Len; }
  67.       void ToLower();  // force all of the string to lower case
  68.       void ToUpper();  // force all of the string to upper case
  69.       int Capacity() const { return Alloc - 1; }
  70.       int Size() const { return (Len+2); } // the current size to store
  71.       int Index(char SearchChar, int StartIndex=0) const;
  72.       int Index(const char* SearchStr, int StartIndex=0) const;
  73.       Bool Find(char SearchChar, int StartIndex=0) const
  74.           {return (Index(SearchChar,StartIndex)!=NotFound);}
  75.       Bool Find(const char* SearchStr, int StartIndex=0) const
  76.           {return (Index(SearchStr,StartIndex)!=NotFound);}
  77.       int Count(char C) const;  // how many of this character occur
  78.  
  79.       void Delete(int Index=0,int Length=1);
  80.       void DeleteLast();
  81.       void Trim();  //   chop off leading and trailing spaces
  82.       void TrimLead();
  83.       void TrimTrail();
  84.       void TrimTrailTo(char C);
  85.         // remove last chars until C is found, then remove C too!
  86.  
  87.       char At(int Index) const;
  88.       char operator()(int Index) const {return At(Index);}
  89.       char Last() const; // the last char in the string
  90.       void Clip(int NewLen);  // chop off the right end of the string if there
  91.  
  92.       String40 Word(int Num=0);
  93.         // using spaces as delimiters, pull out word number 0, 1, 2, etc.
  94.       String40 XWord(int Num=0);
  95.         // same as Word except that the Word is extracted from the original string
  96.  
  97.       #ifdef MAJORBBS
  98.         void* operator new(size_t size){return malloc(size);}
  99.         void  operator delete(void* p) {free(p);}
  100.       #endif
  101.   };
  102.  
  103. class String: public BaseString
  104.   {
  105.     protected:
  106.       void ReNew(int NewCapacity);
  107.       void New(); // Uses Alloc
  108.     public:
  109.       String(const char& C, int L=1);
  110.         // String S(' ',20) constructs a string consisting of 20 spaces
  111.       String(int AllocSize);
  112.         // constructor based on size desired or size not yet known
  113.       String(const char*);
  114.         // a char* type string is used to construct a String type string
  115.       String(const BaseString&);
  116.         // creating one String from another
  117.       String(const String&);
  118.       String();
  119.       ~String() {free(P);}
  120.  
  121.       // assignment operators
  122.       void operator=(const BaseString&);
  123.       void operator=(const char*);
  124.       void operator=(const char);
  125.  
  126.       // String concatination (S1=S2+S3)
  127.       // String operator+(const StackString& S) const;
  128.       String operator+(const String40& S) const;
  129.       String operator+(const String120& S) const;
  130.       String operator+(const String&) const;
  131.       String operator+(const char*) const;
  132.       String operator+(char C) const;
  133.       friend String operator+(const String40&, const String&);
  134.       friend String operator+(const String120&, const String&);
  135.       friend String operator+(const char* CS, const String& S);
  136.       friend String operator+(char C, const String& S);
  137.  
  138.       // Appending stuff to a String
  139.       void operator+=(const BaseString&);
  140.       void operator+=(const char*);
  141.       void operator+=(char);
  142.  
  143.       void Left(int NewSize);  //  resize string and left justify text
  144.       void Right(int NewSize);
  145.       void Center(int NewSize);
  146.       void Just(int Type, int NewSize);
  147.         //  parses out to Left, Right or Center
  148.       void Tail(int NewSize); // works like "Right" cept left chars may be chopped
  149.  
  150.       int ReAlloc(int NewCapacity);
  151.       String At(int Index, int Length) const;
  152.       String operator()(int Index, int Length) const {return At(Index,Length);}
  153.       char   At(int Index) const {return BaseString::At(Index);}
  154.       char   operator()(int Index) const {return BaseString::At(Index);}
  155.       String Before(int Index) const {return At(0,Index);}
  156.       String Through(int Index) const {return At(0,Index+1);}
  157.       String From(int Index) const {return At(Index,Len-Index);}
  158.       String After(int Index) const {return At(Index+1,Len-Index-1);}
  159.  
  160.       void Insert(char C,int Index=0);
  161.       void Insert(const char* St,int Index=0);
  162.       void Replace(const char* SearchStr, const char* ReplaceStr);
  163.       void Replace(const char SearchChar, const char* ReplaceStr);
  164.       void Replace(const char* SearchStr, const char ReplaceChar);
  165.       void Replace(const char SearchChar, const char ReplaceChar);
  166.   };
  167.  
  168. class StackString: public BaseString
  169.   {
  170.     protected:
  171.       friend String;
  172.     public:
  173.  
  174.       void operator=(const BaseString&);
  175.       void operator=(const char*);
  176.       void operator=(const char);
  177.  
  178.       void operator+=(const BaseString&);
  179.       void operator+=(const char*);
  180.       void operator+=(char);
  181.  
  182.       void Left(int NewSize);  //  resize string and left justify text
  183.       void Right(int NewSize);
  184.       void Center(int NewSize);
  185.       void Just(int Type, int NewSize);
  186.       void Tail(int NewSize); // works like "Right" cept left chars may be chopped
  187.  
  188.       void Sub(StackString& D, int Index, int Length);
  189.  
  190.       void Insert(char C,int Index=0);
  191.       void Insert(const char* St,int Index=0);
  192.   };
  193.  
  194. class String40: public StackString
  195.   {
  196.     protected:
  197.       char Buf[41];
  198.       friend String;
  199.     public:
  200.       String40();
  201.       String40(const char& C, int L=1);
  202.       String40(const char*);
  203.       String40(const BaseString&);
  204.       String40(const String40&);
  205.  
  206.       String40 operator+(const String40& S) const;
  207.       String40 operator+(const char* S) const;
  208.       String40 operator+(char C) const;
  209.       friend String40 operator+(const char* S1, const String40& S2);
  210.       friend String40 operator+(char C, const String40& S);
  211.  
  212.       String40 At(int Index, int Length) const;
  213.       String40 operator()(int Index, int Length) const {return At(Index,Length);}
  214.       char   At(int Index) const {return BaseString::At(Index);}
  215.       char   operator()(int Index) const {return BaseString::At(Index);}
  216.       String40 Before(int Index) const {return At(0,Index);}
  217.       String40 Through(int Index) const {return At(0,Index+1);}
  218.       String40 From(int Index) const {return At(Index,Len-Index);}
  219.       String40 After(int Index) const {return At(Index+1,Len-Index-1);}
  220.       void Replace(const char* SearchStr, const char* ReplaceStr);
  221.       void Replace(const char SearchChar, const char* ReplaceStr);
  222.       void Replace(const char* SearchStr, const char ReplaceChar);
  223.       void Replace(const char SearchChar, const char ReplaceChar);
  224.   };
  225.  
  226. class String120: public StackString
  227.   {
  228.     protected:
  229.       char Buf[121];
  230.       friend String;
  231.     public:
  232.       String120();
  233.       String120(const char& C, int L=1);
  234.       String120(const char*);
  235.       String120(const BaseString&);
  236.       String120(const String120&);
  237.  
  238.       String120 operator+(const String40& S) const;
  239.       String120 operator+(const String120& S) const;
  240.       String120 operator+(const char* S) const;
  241.       String120 operator+(char C) const;
  242.       friend String120 operator+(const String40& S1, const String120& S2);
  243.       friend String120 operator+(const char* S1, const String120& S2);
  244.       friend String120 operator+(char C, const String120& S);
  245.  
  246.       String120 At(int Index, int Length) const;
  247.       String120 operator()(int Index, int Length) const {return At(Index,Length);}
  248.       char   At(int Index) const {return BaseString::At(Index);}
  249.       char   operator()(int Index) const {return BaseString::At(Index);}
  250.       String120 Before(int Index) const {return At(0,Index);}
  251.       String120 Through(int Index) const {return At(0,Index+1);}
  252.       String120 From(int Index) const {return At(Index,Len-Index);}
  253.       String120 After(int Index) const {return At(Index+1,Len-Index-1);}
  254.       void Replace(const char* SearchStr, const char* ReplaceStr);
  255.       void Replace(const char SearchChar, const char* ReplaceStr);
  256.       void Replace(const char* SearchStr, const char ReplaceChar);
  257.       void Replace(const char SearchChar, const char ReplaceChar);
  258.   };
  259.  
  260. String40 Str(long);  //  convert integers to plain, unformatted strings
  261. String40 CommaStr(long);
  262. String40 DStr(double Num); // very slow yet works outside of long range
  263. String40 HexStr(Long Num);
  264. String40 OctalStr(Long Num);
  265. String40 Form(const char* Format,double Val);
  266. String40 Form(int FLen,SLong Val);
  267.   // will someday be much faster than using double
  268. String40 Form(int FLen,const double& Val);
  269. String40 Form(int FLen,const Long& Val);
  270. String40 Form(int FLen,const float& Val);
  271. inline String40 Form(int FLen,const int& Val){return Form(FLen,double(Val));}
  272.  
  273. String StringOf(int HowMany, char What);
  274. String Spaces(int HowMany);
  275.  
  276. String LeftText(const char* CS, int NewSize);
  277. String RightText(const char* CS, int NewSize);
  278. String CenterText(const char* CS, int NewSize);
  279. String JustText(const char* CS, int Type, int NewSize);
  280. String TailText(const char* CS, int NewSize);
  281. String Trim(const char* S);
  282. String TrimLead(const char* S);
  283. String TrimTrail(const char* S);
  284. inline String ToLower(const char* S){String St=S; St.ToLower(); return St;}
  285. inline String ToUpper(const char* S){String St=S; St.ToUpper(); return St;}
  286.  
  287. String120 SetFileExtension(const char* OrigName, const char* NewExtension);
  288. String40  FileExtension(const char*);  // returns empty string if no extension
  289. char      DriveLetter(const char*); // current drive returned if no drive specified
  290. String120 SetFileDrive(const char*);
  291. String120 FilePath(const char*);
  292. String120 FileDriveAndPath(const char*);
  293. String120 VerboseFileName(const char*);
  294. String40  RootFileName(const char*); // no extension, drive or path
  295. String40  FileNameAndExtension(const char*);
  296. String120 CleanPath(const char*); // makes sure there is a '\\' as last char
  297.  
  298. typedef String (* StringFuncPtr)(void);
  299. typedef String40 (* String40FuncPtr)(void);
  300. typedef String120 (* String120FuncPtr)(void);
  301.  
  302. #endif
  303.